﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using ifunction.LifeVC.CheckInService;

namespace ifunction.LifeVC.CheckInServiceHost
{
    public partial class LifeVCServiceHost : ServiceBase
    {
        /// <summary>
        /// The service host
        /// </summary>
        protected ServiceHost serviceHost = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceHost"/> class.
        /// </summary>
        public LifeVCServiceHost()
        {
            InitializeComponent();
#if DEBUG
            OnStart(null);
#endif
        }

        /// <summary>
        /// When implemented in a derived class, executes when a Start command is sent to the service by the Service Control Manager (SCM) or when the operating system starts (for a service that starts automatically). Specifies actions to take when the service starts.
        /// </summary>
        /// <param name="args">Data passed by the start command.</param>
        protected override void OnStart(string[] args)
        {
            // Try to stop running service.
            OnStop();

            // Create a ServiceHost for the type and 
            // provide the base address.
            serviceHost = new ServiceHost(typeof(LifeVCService));

            // Open the ServiceHostBase to create listeners and start 
            // listening for messages.
            serviceHost.Open();

            LifeVCService.TrigMonitor();
        }

        /// <summary>
        /// When implemented in a derived class, executes when a Stop command is sent to the service by the Service Control Manager (SCM). Specifies actions to take when a service stops running.
        /// </summary>
        protected override void OnStop()
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
                serviceHost = null;
            }
        }
    }
}

